package com.cmict.data.form.api.util;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cmict.data.form.api.entity.dataobject.SysFormDO;
import com.cmict.data.form.api.entity.vo.SysFromSeachDataVo;
import com.cmict.data.form.api.service.SysCurrencyService;
import com.cmict.data.form.api.service.SysFormService;
import com.cmict.data.form.controller.admin.SysCurrencyController;
import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.infra.api.rdb.RdbApi;
import com.cmict.data.system.api.dept.DeptApi;
import com.cmict.data.system.api.dept.PostApi;
import com.cmict.data.system.api.dept.dto.DeptRespDTO;
import com.cmict.data.system.api.dept.dto.PostDTO;
import com.cmict.data.system.api.dict.DictDataApi;
import com.cmict.data.system.api.dict.dto.DictDataDTO;
import com.cmict.data.system.api.user.AdminUserApi;
import com.cmict.data.system.api.user.dto.AdminUserRespDTO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;

import static com.cmict.data.framework.common.pojo.CommonResult.success;

/**
 * @description: 搜索时间补全工具类
 * @author: qiaoyu
 * @create: 2023-04-08 22:57
 **/
@Component
public class DateChangeUtil {
    private static final Logger logger = LoggerFactory.getLogger(SysCurrencyController.class);

    @Autowired
    private SysFormService sysFormService;
    private static SysFormService realsysFormService;

    @Autowired
    private SysCurrencyService sysCurrencyService;
    private static SysCurrencyService realsysCurrencyService;

    @Autowired
    private DictDataApi dictDataService;
    private static DictDataApi realdictDataService;

    @Autowired
    private DeptApi deptService;
    private static DeptApi realdeptService;

    @Autowired
    private AdminUserApi adminUserService;
    private static AdminUserApi realadminUserService;

    @Autowired
    private PostApi postService;
    private static PostApi realpostService;

    @Autowired
    private RdbApi rdbApiService;
    private static RdbApi realrdbapiService;


    @PostConstruct
    public void init() {
        realsysFormService = sysFormService;
        realsysCurrencyService = sysCurrencyService;
        realdictDataService = dictDataService;
        realdeptService = deptService;
        realadminUserService = adminUserService;
        realpostService = postService;
        realrdbapiService = rdbApiService;
    }

    /**
     *映射数据替换通用方法
     */
    public static SysFromSeachDataVo dateMapping(SysFormDO sysFormDO, SysFromSeachDataVo sysFromSeachData) {
        Map<String,Object> mapdata = sysFromSeachData.getSeachData();
        if(ObjectUtil.isNotEmpty(mapdata)){
            JSONObject personData = JSON.parseObject(sysFormDO.getFormJsonUser());
            JSONArray fields = personData.getJSONArray("fields");
            if (ObjectUtil.isNotEmpty(fields)) {
                for (int j = 0; j < fields.size(); j++) {
                    JSONObject field = fields.getJSONObject(j);
                    String model = field.getString("__vModel__");
                    if(ObjectUtil.isNotEmpty(mapdata.get(model))){
                        JSONObject config = fields.getJSONObject(j).getJSONObject("__config__");
                        String tagIcon = config.getString("tagIcon");
                        String value = map(field, tagIcon, String.valueOf(mapdata.get(model)));
                        sysFromSeachData.getSeachData().put(String.valueOf(model),value);
                    }
                }

            }else {
                JSONObject config = personData.getJSONObject("__config__");
                JSONArray childrenList = config.getJSONArray("children");
                for (int j = 0; j < childrenList.size(); j++) {
                    JSONObject object = (JSONObject) childrenList.get(j);
                    JSONObject configchild = object.getJSONObject("__config__");
                    JSONArray childfields = configchild.getJSONArray("children");
                    for (int k = 0; k < childfields.size(); k++) {
                        JSONObject children = (JSONObject) childfields.get(k);
                        String modelchild = children.getString("__vModel__");
                        JSONObject configchildtwo = children.getJSONObject("__config__");
                        String tagIcon = configchildtwo.getString("tagIcon");
                        if(ObjectUtil.isNotEmpty(mapdata.get(modelchild))){
                            String value = map(children, tagIcon, String.valueOf(mapdata.get(modelchild)));
                            sysFromSeachData.getSeachData().put(String.valueOf(modelchild),value);
                        }
                    }
                }
            }
        }
      return sysFromSeachData;
    }


    static String map(JSONObject field, String tagIcon ,String value) {
        //时间组件
        if("date".equals(tagIcon)) {
            String type = field.getString("format");
            if(!"null".equals(value) && !value.contains(",")){
                if ("yyyy".equals(type)) {
                    value = value + "-01-01";
                }
                if ("yyyy-MM".equals(type)) {
                    value = value + "-01";
                }
            }
        }
        return value;
    }

}

